﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace wmdata
{
    public class password : mysqlconn
    {
        public password(DataTable ptable)
        {
            p = ptable;
            this.Load += new EventHandler(password_Load);
        }

        void password_Load(object sender, EventArgs e)
        {
            passpage.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(passpage_DocumentCompleted);
            passpage.AllowNavigation = true;
            //DataRow[] row = p.Select("date = max(date)");
            //int rowid = (int)row[0]["idp"];
            //if (rowid != 12)
            //{
            //    newrow = p.Rows.Find(rowid + 1);
            //    newrow["date"] = today;
            //    //updatetables(p);
            //}
            //else if (rowid == 12)
            //{
            //    newrow = p.Rows.Find(1);
            //    newrow["date"] = today;
            //    updatetables(p);
            //}
            //byte[] postdata = GetHash("oldpassword=" + row[0]["pass"].ToString() + "&newpassword=" + newrow["pass"].ToString() + "&newpassword2=" + newrow["pass"].ToString());
            //byte[] oldpass = GetHash(row[0]["pass"].ToString());
            //byte[] newpass = GetHash(newrow["pass"].ToString());

            //passpage.Navigate("https://wmt.quickbase.com/db/main?a=DoProfilePasswordChange", false);
            //string url = "https://wmt.quickbase.com/db/main?a=DoProfilePasswordChange=";
            passpage.Navigate("https://wmt.quickbase.com/db/main?a=ProfileView", false);
            //string targetframe = "";
            //string headers = "Content-Type: application/x-www-form-urlencoded" + Environment.NewLine;
            
            //passpage.Navigate(url, targetframe, postdata, headers);
        }

        public static string GenerateHash(string key, string input)
        {
            key = Encoding.Default.GetString(Convert.FromBase64String(key));
            using(var kMd5 = new HMACMD5(Encoding.ASCII.GetBytes(key))) {byte[] hash1 = kMd5.ComputeHash(Encoding.ASCII.GetBytes(input));
            key = BitConverter.ToString(hash1).ToLower().Replace("-","");
            return Convert.ToBase64String(Encoding.ASCII.GetBytes(key));}
        }







        void passpage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (post == false)
            {
                DataRow[] row = p.Select("date = max(date)");
                int rowid = (int)row[0]["idp"];
                if (rowid != 12)
                {
                    newrow = p.Rows.Find(rowid + 1);
                    newrow["date"] = today;
                    //updatetables(p);
                }
                else if (rowid == 12)
                {
                    newrow = p.Rows.Find(1);
                    newrow["date"] = today;
                    updatetables(p);
                }



                string cookie = passpage.Document.Cookie;
                HtmlElement form = passpage.Document.GetElementById("changePasswordForm");
                string[] data = form.InnerHtml.ToString().Split(new string[] { "value=", " " }, StringSplitOptions.None);
                GenerateHash(data[2], "oldpassword=" + row[0]["pass"].ToString() + "&newpassword=" + newrow["pass"].ToString() + "&newpassword2=" + newrow["pass"].ToString());


                HtmlElement oldpass = passpage.Document.GetElementById("oldpassword");
                HtmlElement newpass = passpage.Document.GetElementById("newpassword");
                HtmlElement newpass2 = passpage.Document.GetElementById("newpassword2");
                //postpage = new post(row, newrow, oldpass,newpass, newpass2);
                //postpage.ShowDialog();



                //HtmlElement submit = passpage.Document.GetElementById("newpaswordValidate");
                oldpass.SetAttribute("value", row[0]["pass"].ToString());
                newpass.SetAttribute("value", newrow["pass"].ToString());
                newpass2.SetAttribute("value", newrow["pass"].ToString());
                form.InvokeMember("submit");
                post = true;
            }
            else if (post == true)
            {
                passpage.Navigate("https://wmt.quickbase.com/db/main?a=DoProfilePasswordChange", false);
            }
            
            //passpage.Navigate("https://wmt.quickbase.com/db/main?a=DoProfilePasswordChange", false);
            //passpage.Show();
            //submit.RaiseEvent("newpaswordValidate");
                       
        }

        public bool post = false;
        public static post postpage;
        DataTable p;
        DataRow[] row;
        DataRow newrow;
        DateTime today = DateTime.Today;
        WebBrowser passpage = new WebBrowser();
    }
}
